//
//  ThirtyfiveViewController.m
//  Depressed.domo
//
//  Created by rimi on 16/5/27.
//  Copyright © 2016年 薛涛. All rights reserved.
//

#import "ThirtyfiveViewController.h"
#import "Contact.h"
#import "Datai6ViewController.h"
static const NSInteger BTN_TAG = 100;
static const NSInteger TEXT_TAG = 200;
@interface ThirtyfiveViewController ()
@property (nonatomic, strong)NSArray *keys;
@property (nonatomic, strong)UITextField *textfield;
@end

@implementation ThirtyfiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.title = @"数据持久化";
    self.view.backgroundColor = [UIColor purpleColor];
    [self initUI];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下" style:UIBarButtonItemStylePlain target:self action:@selector(xxxx:)];
}
- (void)xxxx:(UIBarButtonItem *)sender{
    Datai6ViewController *datai6 = [[Datai6ViewController alloc]init];
    [self.navigationController pushViewController:datai6 animated:YES];
}
- (void)initUI {
    _keys = @[@"name",@"age",@"sex"];
    
    for (int i = 0; i < _keys.count; i ++) {
        
        UILabel *label = [[UILabel alloc]initWithFrame:CRM(0, 70 + (60) * i, 100, 30)];
        
        label.text = [NSString stringWithFormat:@"%@%@",_keys[i],@":"];
        
        label.textAlignment = NSTextAlignmentCenter;
        
        label.font = [UIFont systemFontOfSize:25];
        
        label.backgroundColor = [UIColor brownColor];
        
        [self.view addSubview:label];
        
         _textfield= [[UITextField alloc]initWithFrame:CRM(CGRectGetMaxX(label.frame) + 10, CGRectGetMinY(label.frame), 150, 30)];
        
        _textfield.tag = TEXT_TAG + i;
        _textfield.keyboardType = UIKeyboardTypeASCIICapable;
        _textfield.borderStyle = UITextBorderStyleLine;
        
        [self.view addSubview:_textfield];
    }
    
    NSArray *btnArray = @[@"save",@"read",@"clear"];
    for (int i = 0; i < btnArray.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.bounds = CRM(0, 0 , 80, 30);
        btn.center = CPM(80 +CGRectGetWidth(self.view.bounds) / 4 * i, 300);
        btn.tag = BTN_TAG + i;
        btn.backgroundColor = [UIColor blueColor];
        [btn setTitle:btnArray[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
}

- (void)btn:(UIButton *)sender{
    NSInteger index = sender.tag - BTN_TAG;
    switch (index) {
        case 0:
            [self save];
            break;
        case 1:
            [self read];
            break;
        case 2:
            [self clear];
            break;
            
        default:
            break;
    }
    /**回收键盘*/
    [_textfield resignFirstResponder];
}
/**保存*/
- (void)save{
   /**获取联系人*/
    Contact *con = [[Contact alloc]initWithDictionary:[self contactInfomation]];
    
    /**获取文件路径*/
    NSString *plistpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    plistpath = [plistpath stringByAppendingString:@"/data.plist"];
    NSLog(@"%@",plistpath);
    /**初始化写入沙盒的数组*/
    NSMutableArray *dataLists = [NSMutableArray arrayWithArray:[NSArray arrayWithContentsOfFile:plistpath]];
    /**归档*/
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:con];
    /**归档得到数据添加数组*/
    [dataLists addObject:data];
    /**数据写入沙盒*/
    BOOL secess = [dataLists writeToFile:plistpath atomically:YES];
    NSLog(@"是否保存成功 %d",secess);
}

/**读取*/
- (void)read{
/**获取保存文件*/
    NSString *plistpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    plistpath = [plistpath stringByAppendingString:@"/data.plist"];
    /**读取沙盒文件*/
    NSArray *result = [NSArray arrayWithContentsOfFile:plistpath];
    /**解档*/
    NSData *data1 = result[0];
    Contact *con = [NSKeyedUnarchiver unarchiveObjectWithData:data1];
    NSLog(@"%@",con);
}
/**删除联系人*/
- (void)clear {
    /**获取保存文件路径*/
    NSString *plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    plistPath = [plistPath stringByAppendingString:@"/data.plist"];
    /**删除沙盒文件*/
    NSFileManager *manger = [NSFileManager defaultManager];
    NSError *error = nil;
    BOOL sucess = [manger removeItemAtPath:plistPath error:&error];
    NSLog(@"是否删除成功 %d",sucess);
}
/**取得联系人*/
- (NSDictionary *)contactInfomation {
    NSMutableDictionary *dic =[NSMutableDictionary dictionary];
    for (NSInteger index = 0; index < _keys.count; index ++) {
        UITextField *textfield = [self.view viewWithTag:TEXT_TAG + index];
        if (textfield.text.length > 0) {
            [dic setObject:textfield.text forKey:_keys[index]];
        }
    }
    return dic;
}
@end
